package com.tvserials.notification.adapters;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;
import com.tvserials.notification.fragments.DefaultSerialsFragment;
import com.tvserials.notification.fragments.LostFilmSerialsFragment;
import com.tvserials.notification.fragments.NewStudioSerialsFragment;
import com.tvserials.notification.fragments.SeasonVarSerialsFragment;
import com.tvserials.notification.vo.SourceType;

import java.util.ArrayList;
import java.util.List;

public class TabsPagerAdapter extends FragmentPagerAdapter {

    private final List<DefaultSerialsFragment> fragments;
    private Fragment mCurrentFragment;

    public TabsPagerAdapter(FragmentManager fm, List<SourceType> sourceTypes) {
        super(fm);
        fragments = new ArrayList<DefaultSerialsFragment>();
        for (SourceType sourceType : sourceTypes) {
            if (SourceType.LOST_FILM.equals(sourceType)) {
                fragments.add(new LostFilmSerialsFragment());
            } else if (SourceType.NEW_STUDIO.equals(sourceType)) {
                fragments.add(new NewStudioSerialsFragment());
            } else if (SourceType.SEASON_VAR.equals(sourceType)) {
                fragments.add(new SeasonVarSerialsFragment());
            }
        }
    }

    @Override
    public Fragment getItem(int index) {
        return fragments.get(index);
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        if (mCurrentFragment != object) {
            mCurrentFragment = (Fragment) object;
        }
        super.setPrimaryItem(container, position, object);
    }

    public Fragment getCurrentFragment() {
        return mCurrentFragment;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return fragments.size();
    }
}